package com.masterclient.app.modules.order.contract;

import android.widget.Toast;

import com.eoner.baselib.presenter.ThrowableConsumer;
import com.masterclient.app.base.ProgressLoginDialog;
import com.masterclient.app.httpservice.RetrofitUtil;
import com.masterclient.app.httpservice.RetrofitUtil2;
import com.eoner.baselibrary.bean.order.CheckRefundBean;
import com.eoner.baselibrary.bean.order.OrderDetailBean;
import com.eoner.baselibrary.utils.Utils;

import org.json.JSONException;
import org.json.JSONObject;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import okhttp3.RequestBody;

/**
* Created by TMVPHelper on 2017/07/28
*/
public class OrderDetailPresenter extends OrderDetailContract.Presenter{


    @Override
    public void getOrderDetail(String order_no) {
        Observable<OrderDetailBean> observable = RetrofitUtil.getInstance().orderDetail(order_no);
        Disposable disposable = toSubscribe(observable, new Consumer<OrderDetailBean>() {
            @Override
            public void accept(@NonNull OrderDetailBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.showOrderDetail(bean.getData());
                }else {
                    Toast.makeText(Utils.getContext(),bean.getMsg(),Toast.LENGTH_SHORT).show();
                }
            }
        },new ThrowableConsumer());
        mRxManager.add(disposable);
    }

    @Override
    public void checkRefund(String order_id, String order_product_id, ProgressLoginDialog progressLoginDialog) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("order_id",order_id);
            jsonObject.put("order_product_id",order_product_id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        Observable<CheckRefundBean> observable = RetrofitUtil2.getInstance().checkRefund(body);
        Disposable disposable = toSubscribe(observable, new Consumer<CheckRefundBean>() {
            @Override
            public void accept(@NonNull CheckRefundBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.getCheckRefund(bean);
                }else {
                    progressLoginDialog.dismiss();
                    Toast.makeText(Utils.getContext(),bean.getMsg(),Toast.LENGTH_SHORT).show();
                }
            }
        },new ThrowableConsumer());
        mRxManager.add(disposable);
    }

}